/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uit.ejb.bo.user;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.rowset.WebRowSet;
import uit.ejb.dae.user.UserDAERemote;
import uit.ejb.dae.user.UserDAERemoteHome;
import uit.ejb.dto.UserDTO;

/**
 *
 * @author SVN - Team
 */
public class UserBO implements SessionBean {

    private SessionContext context;

    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }

    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
    }

    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
    }

    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
    }

    // </editor-fold>;
    /**
     * See section 7.10.3 of the EJB 2.0 specification
     * See section 7.11.3 of the EJB 2.1 specification
     */
    public void ejbCreate() {
        // TODO implement ejbCreate if necessary, acquire resources
        // This method has access to the JNDI context so resource aquisition
        // spanning all methods can be performed here such as home interfaces
        // and data sources.
    }

    public String businessMethod() throws NamingException, CreateException, RemoteException, FinderException {
        UserDAERemote us = getUserObject();
        return us.businessMethod();
    }

    public WebRowSet user_GetByAll() {
        try {
            UserDAERemote us = getUserObject();
            return us.user_GetByAll();
        } catch (Exception e) {
            return null;
        }
    }

    public boolean user_Insert(UserDTO user) {
        try {
            UserDAERemote us = getUserObject();
            return us.user_Insert(user);
        } catch (Exception e) {
            return false;
        }
    }

    public int user_Delete(String id) {
        try {
            UserDAERemote us = getUserObject();
            return us.user_Delete(id);
        } catch (Exception e) {
            e.getMessage();
            return 0;
        }
    }

    public boolean user_Update(UserDTO user) {
        try {
            UserDAERemote us = getUserObject();
            return us.user_Update(user);
        } catch (Exception e) {
            e.getMessage();
            return false;
        }
    }

    public WebRowSet user_GetById(UserDTO user) {
        try {
            UserDAERemote us = getUserObject();
            return us.user_GetById(user);
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    }

    public WebRowSet user_GetByTop(String Top, String Where, String Order) {
        try {
            UserDAERemote us = getUserObject();
            return us.user_GetByTop(Top, Where, Order);
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    }

    public int user_CheckLogin(String user, String pass) {
        try {
            UserDAERemote us = getUserObject();
            return us.user_CheckLogin(user, pass);
        } catch (Exception e) {
            System.out.print(e);
            return -1;
        }
    }

    private UserDAERemote getUserObject() throws RemoteException, ClassCastException, FinderException, NamingException, CreateException {
        Context ctx = new InitialContext();
        Object obj = ctx.lookup("ejb/UserDAE");
        UserDAERemoteHome phome = (UserDAERemoteHome) PortableRemoteObject.narrow(obj, UserDAERemoteHome.class);
        UserDAERemote us = phome.create();
        return us;
    }
}
